<?
if( !defined( 'MODULE_FILE' ) ) {
	die( "cant access file directly!" );
}

autoLoader();

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );
$emp_id = ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) && true == valStr( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : NULL;

$objPatient 	= new BasePatients();
$objPatients 	= new Patients();
$objCasePaper 	= new CasePapers();
$objTemplate 	= new Templates();

?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>

	<link rel="stylesheet" type="text/css" media="screen" href="js/Autocomplete/resources/css/smoothness/jquery-ui-1.10.1.custom.css"/>
	<link rel="stylesheet" type="text/css" media="screen" href="js/Autocomplete/resources/css/smoothness/jquery.ui.combogrid.css"/>
	<link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
	
	<script type="text/javascript" src="js/Autocomplete/resources/jquery/jquery-ui-1.10.1.custom.min.js"></script>
	<script type="text/javascript" src="js/Autocomplete/resources/plugin/jquery.ui.combogrid-1.6.3.js"></script>
	<script src="js/plugins/dataTables/jquery.dataTables.js"></script>
	<script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>

</head>
<body>
<br>
<div class="col-lg-10">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-bell fa-fw"></i>Patient Record (IPD)</div>
<div class="panel-body">
<?php
		$intPatientId 			= '';
		$intCasePaperId			= '';
		$strName 				= '';
		
		if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
			$intPatientId = $_GET['patient_id'];
			
			$objPatient 	= $objPatients->fetchPatientsById( $intPatientId );
			$objCasePaper 	= $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
			
			if( true == valObj( $objPatient, 'BasePatients' ) ) {
				$strName = $objPatient->getFirstName() . ' ' . $objPatient->getLastName();
			}
			
			if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
				$intCasePaperId = $objCasePaper->getId();
				$strFollowupDate = date( 'd-m-Y', strtotime( $objCasePaper->getFollowupDate() ) );
			}
		}
?>
<!-- action="modules.php?file=insert_opd_case_paper&type=insert&action=insert&mid=<? //echo $mid; ?>&smid=<? //echo $smid; ?>" -->
	<form METHOD="POST" action="#" id="formSubmit" name="formSubmit" >
		<div>
			<div style="float: left;">
				Patient Details:- <input type="text" name="patient_details" id="patient_details" value="<?php echo $intPatientId; ?>" class="validate[required]" placeholder="ID or Patient name.." style="width:400px;"/>
				<input type="hidden" name="patient_id" id="patient_id" value="<?php echo $intPatientId; ?>" />
				<input type="hidden" name="case_paper_id" id="case_paper_id" value="<?php echo $intCasePaperId; ?>" />
				<input type="button" name="search_patient" class="btn btn-danger" value="SEARCH"  onclick="searchPatient()" />
				&nbsp;&nbsp;&nbsp;
			</div>
			<div style="display: <?php echo ( true == valStr( $strName ) ) ? 'block;' : 'none;'; ?>" id="success_display">
				<img alt="Patient Found with info.." src="images/green_tick.gif" width="40" height="40">
				<label id='disp_patient_name'><?php echo '(' . $intPatientId . ') ' . $strName; ?></label>
			</div>
			<div style="display: none;" id="failed_display">
				<img alt="Patient Found with info.." src="images/delete.png" width="40" height="40">
				<label id='disp_failed'></label>
			</div>
		</div>
		<div style="clear: both;"></div>
		<div id="patient_info"></div>
		<hr/>

<!--*******************************************************/ -->		
		<div id="opd_case_paper_details" style="border-style: solid; display: none;">
			<table width="100%" cellspacing="0" cellpadding="3" style="margin-left: px;" class="table table-striped table-bordered table-hover dataTable no-footer">
				<tr valign="top">
					<td class="mandatory">
						Date Of Admission: <?php echo date( 'd-M-Y :: H:m:s' );?>
					</td>
					<td>
						<div>Relieved/Expired/AMA/</div>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						Diagnosis Details:- <?php echo ( true == valStr( $strDiagnosisDetails ) ) ? $strDiagnosisDetails : 'This the Test details of the Disgnosis'; ?>
					</td>
				</tr>
				<tr>
					<td>
						Wards Info:- <?php echo ( true == valStr( $strDiagnosisDetails ) ) ? $strDiagnosisDetails : 'Ward infor is here'; ?>
					</td>
					<td>
						Charge Per Day:- <?php echo ( true == valStr( $strDiagnosisDetails ) ) ? $strDiagnosisDetails : 'Ward infor is here'; ?>
					</td>
				</tr>
				<tr>
					<td colspan="2" style="background-color: gray;">Yellow Page Info</td>
				</tr>
				
				<tr>
					<td colspan="2">
						<div id="yellow_page_details" style="width: 100%;">
							<table style="width: 100%;" id="yellow_page_table">
								<tr style="text-align: center;">
									<td class="td_border_3_yellow">Investigations Advised</td>
									<td class="td_border_3_yellow">Doctors Note</td>
								</tr>
								<tr style="border-bottom: solid;">
									<td class="td_border_middle_yellow">
										<input type="text" name="investigation_1" id="investigation_1" placeholder="Add Investigation Note here..." />
									</td>
									<td>
										<div style="float: left;">
											<textarea style="margin-left: 10px;" rows="3" cols="30" name="doctor_note_1" id="doctor_note_1" placeholder="Add note here for today's check up...."></textarea>
										</div>
										<div>
<!-- 											<img src="images/icons/add_new.png" width="45" height="45" /> -->
										</div>
									</td>
								</tr>
								<tr style="border-bottom: solid;">
									<td class="td_border_middle_yellow">
										<input type="text" name="investigation_1" id="investigation_1" placeholder="Add Investigation Note here..." />
									</td>
									<td>
										<div style="float: left;">
											<textarea style="margin-left: 10px;" rows="3" cols="30" name="doctor_note_1" id="doctor_note_1" placeholder="Add note here for today's check up...."></textarea>
										</div>
										<div>
											<img src="images/icons/add_new.png" width="45" height="45" />
											<img src="images/icons/delete.jpg" width="45" height="45" />
										</div>
									</td>
								</tr>
							</table>
						</div>
					</td>
				</tr>
				
				<tr><td colspan="2">&nbsp;</td></tr>
				
				<tr>
					<td colspan="2">
						<div style="float: left;">Remark Of Patient:-</div>
						<div><textarea style="resize: vertical;" rows="3" cols="25"></textarea></div>
					</td>
				</tr>
				<tr valign="top">
					<td>
						<?php 
							if( true == valStr( $strName ) ) { ?>
								<input type="hidden" name="operation" id="operation" value="update" />
								<input type="submit" name="Submit" id="submit" value="UPDATE" class="btn btn-primary">
								<input type="button" name="cancel" id="cancel" value="Cancel" class="btn btn-primary">
								<a href="modules.php?file=print_opd_case_paper&type=print&patient_id=<?php echo $intPatientId; ?>&case_paper_id=<?php echo $intCasePaperId; ?>&mid=<?php echo $mid;?>&smid=<?php echo $smid;?>"><input type="button" name="print" id="print" value="PRINT"></a>
								<a href="modules.php?file=opd_case_paper&mid=<?php echo $mid;?>&smid=<?php echo $smid;?>"><input type="button" name="cancel" id="cancel" class="btn btn-primary" value="Cancle"></a>
						<?php 	} else { ?>
								<input type="hidden" name="operation" id="operation" value="insert" />
								<input type="botton" name="Submit" id="submit" value="SUBMIT" class="btn btn-primary">
								<input type="button" name="cancel" id="cancel" value="Cancel" class="btn btn-primary">
								<input type="hidden" name="print" id="print" value="PRINT">
								<input type="hidden" name="print" id="print_preview" value="PRINT PREVIEW">
						<?php } ?>
						<a id="print_opd_form" style="display: none;" href="modules.php?file=print_opd_case_paper&type=print&mid=<?php echo $mid;?>&smid=<?php echo $smid;?>"><input type="button" name="print" id="print" class="btn btn-primary" value="PRINT"></a>
					</td>
				</tr>
			</table>
		</div>
</div>
</div>
</div>	
</form>
</body>
<script>
	$(document).ready(function(){
		$( "#from" ).datepicker({
			dateFormat: 'dd-mm-yy',
			showOn: "button",
			buttonImage: "images/calendar.gif",
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			yearRange: ":+1"
		});
	});

	jQuery(document).ready(function(){
		$( "#patient_details" ).combogrid({
			url: 'modules.php?file=fetch_server&type=fetch&action=serachpatientbycasepaper',
			debug:true,
			//replaceNull: true,
			colModel: [{'columnName':'id','width':'10','label':'Id'}, {'columnName':'name','width':'60','label':'Full Name'},{'columnName':'case_paper_id','width':'30','label':'Case Paper Id'}],
			select: function( event, ui ) {
				$( "#patient_details" ).val( ui.item.name );
				$( "#patient_id" ).val( ui.item.id );
				$( "#case_paper_id" ).val( ui.item.case_paper_id );
				return false;
				}
		});
	});

	function searchPatient() {

		var intPatientId =	$('#patient_details').val();

		$strUrl = "modules.php?file=search_patient&type=search&patient_data=" + intPatientId;

		var intCasePaperId = '';

		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			success: function( returnData ) {
				strCustomerData = returnData;
		
				if( 'not found::' == strCustomerData ) {
					$( '#failed_display' ).css( "display", "block" );
					$( '#disp_failed' ).html( "Patient not found with :: " + $('#patient_details').val() );
					$( '#success_display' ).css( "display", "none" );
					$( '#patient_id' ).val( '' );
					$( '#case_paper_id' ).val( '' );

					emptyData();
				} else {
					$( '#failed_display' ).css( "display", "none" );
					$( '#success_display' ).css( "display", "block" );

					var strData = strCustomerData.split( '_' );
					
					$( '#disp_patient_name' ).html( "(" + strData[0] + ") " + strData[1] + " " + strData[2] );
					$( '#patient_id' ).val( strData[0] );
					$( '#case_paper_id' ).val( strData[3] );

					$('#opd_case_paper_details').show();
					intCasePaperId = strData[3];

					//fetchTemplateData( intCasePaperId );
					displayPatientInfo( strData[0] );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function fetchTemplateData( intCasePaperId ) {

		var intPatientId 	=	$('#patient_id').val();

		var getParams={ patient_id:intPatientId };
		 
		$.getJSON( 'modules.php?file=fetch_case_papers&type=fetch&case_paper_id=' + intCasePaperId, getParams,
		function( data ) {
			if( data != 'not found::' ) {
				$( '#cheif_complaints' ).text( data[0]['data'] );
				$( '#prescription' ).text( data[1]['data'] );
				$( '#diagnosis' ).text( data[2]['data'] );
				$( '#investigation' ).text( data[3]['data'] );
				$( '#remark' ).text( data[4]['data'] );

				// set template ids to respeative text areas.
				$( '#cheif_complaints_template_id' ).val( data[0]['id'] );
				$( '#diagnosis_template_id' ).val( data[1]['id'] );
				$( '#prescription_template_id' ).val( data[2]['id'] );
				$( '#investigation_template_id' ).val( data[3]['id'] );
				$( '#remark_template_id' ).val( data[4]['id'] );
				$( '#from' ).val( data[4]['follow_date'] );

				$( '#operation' ).val( 'update' );
				$( '#submit' ).val( 'UPDATE' );

				$( '#print_opd_form' ).show();
				var href = $('#print_opd_form').attr("href");
				$('#print_opd_form').attr( "href", href + '&patient_id=' + intPatientId + '&case_paper_id=' + intCasePaperId );
				
			} else {
				$( '#cheif_complaints' ).text( '' );
				$( '#prescription' ).text( '' );
				$( '#diagnosis' ).text( '' );
				$( '#investigation' ).text( '' );
				$( '#remark' ).text( '' );
				$( '#from' ).val( '' );

				// set template ids to respeative text areas.
				$( '#cheif_complaints_template_id' ).val( '' );
				$( '#diagnosis_template_id' ).val( '' );
				$( '#prescription_template_id' ).val( '' );
				$( '#investigation_template_id' ).val( '' );
				$( '#remark_template_id' ).val( '' );

				$( '#operation' ).val( 'insert' );
				$( '#submit' ).val( 'SUBMIT' );
				
				$( '#print_opd_form' ).hide();
			}
		});
	}

	function displayPatientInfo( intPatientId ) {

		$strUrl = 'modules.php?file=patient_info&type=search&patient_id=' + intPatientId;
		
		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			success: function( returnData ) {
				strPatientData = returnData;
		
				if( 'not found::' == strPatientData ) {
					$( '#patient_info' ).html( '' );
					$( '#patient_info' ).css( "display", "none" );
				} else {
					$( '#patient_info' ).html( strPatientData );
					$( '#patient_info' ).css( "display", "block" );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function emptyData() {
		$('#opd_case_paper_details').hide();

		$( '#patient_info' ).html( '' );
		$( '#patient_info' ).css( "display", "none" );
		$( '#opd_case_paper_details' ).hide();
	}
		
</script>
<style type="text/css">
	.ui-datepicker-year {
		background-color: #f6a828;
	}
	.ui-datepicker-month {
		background-color: #f6a828;
	}
	.text_area {
		resize:none;
	}
	
	.cg-menu {
		width: 400px;
	}
	
	.cg-menu-item {
		height: 34px;
		font-size: 17px;
	}
	.cg-comboButton {
		height: 50px;
		font-size: 20px;
	}
	
	.td_border_3_yellow {
		width: 30%;
		border-right: solid;
		border-bottom: solid;
		border-left: solid;
	}
	
	.td_border_middle_yellow {
		width: 30%;
		border-right: solid;
	}
</style>

</html>